package com.koron.device.zsjRemote.controller;


import com.github.pagehelper.PageHelper;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.device.bean.Equipment;
import com.koron.device.service.EquipmentService;
import com.koron.device.zsjRemote.bean.CollaborationRecord;
import com.koron.device.zsjRemote.feign.AttachmentFeignService;
import com.koron.device.zsjRemote.service.ZsjRemoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RequestMapping("/ZsjRemote")
@RestController
@Api(tags = "珠三角远程协作")
public class ZsjRemoteController {

	@Autowired
	private EquipmentService equipmentservice;

	@Autowired
	private ZsjRemoteService zsjRemoteService;

	@Autowired
	private AttachmentFeignService attachmentFeignService;

	@GetMapping("/getAttachmentById")
	@ApiOperation(value = "根据KKS编码或code或equipment类中的其他元素查找图档资料")
	public Response getAttachmentById(Equipment equipmentWithKks, String businessType, HttpServletRequest httpServletRequest){
		Equipment equipment = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), zsjRemoteService, "getAttachmentById", Equipment.class, equipmentWithKks,httpServletRequest.getHeader("Language"));
		return Response.ok(attachmentFeignService.attachmentList(equipment.getId(),null,businessType));
	}

	@GetMapping("/getByEncode")
	@ApiOperation(value = "根据KKS编码查找设备信息(精确查询)")
	public Response getByEncode(Equipment equipmentId, HttpServletRequest httpServletRequest){
		Equipment equipment = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), zsjRemoteService, "getAttachmentById", Equipment.class, equipmentId,null);
		return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), equipmentservice, "getById", Response.class, equipment.getId(),null,null);
	}

	@GetMapping("/queryByNameAndKKS")
	@ApiOperation(value = "根据名称或kks编码模糊搜索设备信息(模糊查询)")
	public Response<List<Equipment>> queryByNameAndKKS(Equipment equipment, PageQuery pageQuery){
		PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
		return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), zsjRemoteService, "queryByNameAndKKS", Response.class, equipment);
	}

	//根据kks获取设备类型,为了获取工卡
	@GetMapping("/getTypeByKKS")
	@ApiOperation(value = "根据kks获取设备类型")
	public String getTypeByKKS(String encode){
		return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), zsjRemoteService, "getTypeByKKS", String.class, encode);
	}
//	@GetMapping("/queryOrderCardByKKS")
//	@ApiOperation(value = "根据设备KKS编码获取工卡列表(有不同方案)")
//	public Response<List<OrderCard>> queryOrderCardByKKS(String encode){
//		return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(),zsjRemoteService,"queryOrderCardByKKS", Response.class, encode);
//	}

	@GetMapping("/queryCollaborationRecord")
	@ApiOperation(value = "查询所有协作记录列表")
	public Response<List<CollaborationRecord>> queryCollaborationRecord(){
		return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(),zsjRemoteService,"queryCollaborationRecord",Response.class);
	}

/*	@GetMapping("/queryCollaborationRecordByWorker")
	@ApiOperation(value = "根据用户名称,账号查询协作记录列表")
	public Response<List<CollaborationRecord>> queryCollaborationRecordByWorker(String account, String name){
		return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(),zsjRemoteService,"queryCollaborationRecord",Response.class, account, name);
	}*/

	@PostMapping("/startCollaboration")
	@ApiOperation(value = "开始协作,第一步保存协作记录,第二步发送消息")
	public Response startCollaboration(@RequestBody CollaborationRecord record){
		return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(),zsjRemoteService,"startCollaboration", Response.class, record);
	}
}
